package com.jambo.yudada.scoring;

import com.jambo.yudada.common.ErrorCode;
import com.jambo.yudada.exception.BusinessException;
import com.jambo.yudada.model.entity.App;
import com.jambo.yudada.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScoringStrategyExecutor {
    // 策略列表
   @Resource
    private List<ScoringStrategy> scoringStrategyList;
/**
 * 评分
 */
public UserAnswer doScore(List<String> choiceList, App app)throws Exception{
    Integer appType=app.getAppType();
    Integer scoringStrategy=app.getScoringStrategy();
    if(appType==null||scoringStrategy==null){
        throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有无，为找到匹配的策略");
    }
    //根据注解获取策略
    for (ScoringStrategy strategy:scoringStrategyList){
        if(strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
            ScoringStrategyConfig scoringStrategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
            if(scoringStrategyConfig.appType()==appType&&scoringStrategyConfig.scoringStrategy()==scoringStrategy){
                return strategy.doScore(choiceList,app);
            }
        }
    }
    throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配的策略");
}
}
